package org.chnxi.study.leetcode.questions50.ques26;

import java.util.Arrays;

/**
 * 执行用时：137 ms, 在所有 Java 提交中击败了5.04%的用户
 * 内存消耗：41.6 MB, 在所有 Java 提交中击败了5.74%的用户
 */
public class Simple01 {

    public static void main(String[] args) {
        int[] nums1 = {1, 1, 1, 1, 1, 2};
        int len1 = removeDuplicates(nums1);
        System.out.println("长度："+len1+";"+ Arrays.toString(nums1));

        int[] nums2 = {0,0,1,1,1,2,2,3,3,4};
        int len2 = removeDuplicates(nums2);
        System.out.println("长度："+len2 +";"+Arrays.toString(nums2));

        int[] nums3 = {1,1};
        int len3 = removeDuplicates(nums3);
        System.out.println("长度："+len3);
    }

    public static int removeDuplicates(int[] nums) {
        int loop = 1;
        int diff = 1;
        int len = nums.length;

        while (loop < len){

            int countor = 0;

            while (loop < len && nums[diff-1] == nums[loop]){
                loop++;
                countor++;
            }

            if(countor>0){
                int tmp = loop;
                while (tmp < len){

                    nums[diff + tmp - loop] = nums[tmp];
                    tmp++;

                }
                loop = diff;
                len = len - countor;
                //System.out.println("idx="+loop+";len="+len+";chongfu="+countor+";arrays="+Arrays.toString(nums));
            }else{
                loop++;
                diff++;
            }
        }

        return diff;
    }

}
